﻿using ManagementApi.Models;
using ManagementApi.Models.BigDataStatistics;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.CheckIn.Entitys;
using Modules.Person;
using Modules.Project;
using Modules.Project.Entitys;
using PmSoft;
using PmSoft.ApplicationContext;

namespace ManagementApi.Controllers.ManagerPannel
{
    /// <summary>
    /// 大数据可视化接口Api
    /// </summary>
    [Route("/api/v1/manager/bigDataStatistics")]
    [ApiController]
    [Authorize(Roles = "Manager")]
    public class BigDataStatisticsController : ControllerBase
    {
        private readonly ProjectManagerApplicationContext applicationContext;
        private readonly ProjectService projectService;
        private readonly ProjectGroupMemberService projectGroupMemberService;
        private readonly PersonService personService;


        public BigDataStatisticsController(ProjectManagerApplicationContext applicationContext, ProjectService projectService, ProjectGroupMemberService projectGroupMemberService, PersonService personService)
        {
            this.applicationContext = applicationContext;
            this.projectService = projectService;
            this.projectGroupMemberService = projectGroupMemberService;
            this.personService = personService;
        }

        /// <summary>
        /// 项目分类统计
        /// </summary>
        /// <returns></returns>
        [HttpGet("ProjectClassificationStatistics")]
        public ApiResult<ProjectClassificationStatisticsDto> ProjectClassificationStatistics()
        {
            IEnumerable<ProjectInfo>? projectInfos = projectService.GetProjectInfosByManagerId(applicationContext.CurrentUser?.Id ?? 0);

            ProjectClassificationStatisticsDto dto = new();
            dto.ProjectCount = projectInfos.Count();
            dto.data = projectInfos.GroupBy(a => a.IndustryType).Select(s => new ProjectClassificationStatisticsDataDto
            {
                Count = s.Count(),
                IndustryType = s.Key,
                IndustryTypeName = s.Key.GetDescription()
            });
            return ApiResult<ProjectClassificationStatisticsDto>.Success(dto);
        }

        /// <summary>
        /// 项目用工分析、工种分析
        /// </summary>
        /// <returns></returns>
        [HttpGet("ProjectUseEmployeeStatistics")]
        public ApiResult<ProjectUseEmployeeStatisticsDto> ProjectUseEmployeeStatistics()
        {
            IEnumerable<ProjectInfo>? projectInfos = projectService.GetProjectInfosByManagerId(applicationContext.CurrentUser?.Id ?? 0);

            ProjectUseEmployeeStatisticsDto dtos = new();
            List<ProjectUseEmployeeStatisticsDataDto> datas = new(){
               new (){ Name="26岁以下"},
                new (){ Name="26-30岁"},
                new (){ Name="31-35岁"},
                new (){ Name="36-40岁"},
                new (){ Name="41-45岁"},
                new (){ Name="46-50岁"},
                new (){ Name="51-55岁"},
                new (){ Name="55岁以上"},
            };
            IEnumerable<ProjectGroupMember> projectGroupMembers = projectGroupMemberService.GetProjectGroupMembersByProjectIds(projectInfos.Select(a => a.ProjectId));
            IEnumerable<int> personIds = projectGroupMembers.GroupBy(a => a.PersonId).Select(a => a.Key);
            IEnumerable<PersonInfo> personInfos = personService.GetPersons(personIds);
            foreach (var item in personInfos)
            {
                foreach (var data in datas)
                {
                    if (item.Birthday.HasValue)
                    {
                        int age = item.Birthday.Value.GetAge();
                        if (age < 26 && data.Name == "26岁以下")
                        {
                            if (item.Gender == 1)
                                data.MenCount++;
                            else data.Women++;
                        }
                        else if (age >= 26 && age <= 30 && data.Name == "26-30岁")
                        {
                            if (item.Gender == 1)
                                data.MenCount++;
                            else data.Women++;
                        }
                        else if (age >= 31 && age <= 35 && data.Name == "31-35岁")
                        {
                            if (item.Gender == 1)
                                data.MenCount++;
                            else data.Women++;
                        }
                        else if (age >= 36 && age <= 40 && data.Name == "36-40岁")
                        {
                            if (item.Gender == 1)
                                data.MenCount++;
                            else data.Women++;
                        }
                        else if (age >= 41 && age <= 45 && data.Name == "41-45岁")
                        {
                            if (item.Gender == 1)
                                data.MenCount++;
                            else data.Women++;
                        }
                        else if (age >= 46 && age <= 50 && data.Name == "46-50岁")
                        {
                            if (item.Gender == 1)
                                data.MenCount++;
                            else data.Women++;
                        }
                        else if (age >= 51 && age <= 55 && data.Name == "51-55岁")
                        {
                            if (item.Gender == 1)
                                data.MenCount++;
                            else data.Women++;
                        }
                        else if (age >= 56 && data.Name == "55岁以上")
                        {
                            if (item.Gender == 1)
                                data.MenCount++;
                            else data.Women++;
                        }
                    }
                }
            }

            dtos.WomenCount = datas.Sum(x => x.Women);
            dtos.MenCount = datas.Sum(x => x.MenCount);

            IEnumerable<ProjectUseEmployeeStatisticsWorkTypeDataDto> workTypeData = projectGroupMembers.GroupBy(x => x.Type).Select(a => new ProjectUseEmployeeStatisticsWorkTypeDataDto()
            {
                WorkType = a.Key,
                WorkTypeName = a.Key?.GetDescription() ?? string.Empty,
                PersonCount = a.Count()
            });
            dtos.WorkTypeData = workTypeData;
            return ApiResult<ProjectUseEmployeeStatisticsDto>.Success(dtos);
        }


        //public ApiResult<ProjectStatisicsDto> ProjectStatisics()
        //{
        //    IEnumerable<ProjectInfo>? projectInfos = projectService.GetProjectInfosByManagerId(applicationContext.CurrentUser?.Id ?? 0);
            
        //    foreach (var projectInfo in projectInfos)
        //    {

        //    }
        //}

    }
}
